package com.itheima.config;


import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

//TODO:配置SqlSessionFactory和MapperScannerConfigurer
public class MybatisConfig {

    /*
           SqlSessionFactoryBean (mybatis的spring整合包提供的类)
           1. 底层会提供 SqlSessionFactory
           2. 当开发者需要注入dao层bean的,此SqlSessionFactory会自动提供, 省略过程变量
               1). 还需要设置dao层的扫描
           3.替代了MybatisFactory+ mybatis的核心配置
        */
    @Bean
    public SqlSessionFactoryBean getSqlSessionFactoryBean(DataSource ds){
        SqlSessionFactoryBean factoryBean = new SqlSessionFactoryBean();
        //设置pojo的包扫描
        factoryBean.setTypeAliasesPackage("com.itheima.domain");
        //设置连接池
        factoryBean.setDataSource(ds);

        return factoryBean;
    }
    //定义bean，返回MapperScannerConfigurer对象(mybatis的spring整合包提供的类)
    @Bean
    public MapperScannerConfigurer mapperScannerConfigurer(){
        MapperScannerConfigurer msc = new MapperScannerConfigurer();
        //设置dao层的接口扫描
        msc.setBasePackage("com.itheima.mapper");
        return msc;
    }
}
